package com.lonely.fx.登录页案例;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.Objects;

/**
 * @date: 2022-07-06 15:31
 * @author: ztkj-hzb
 * @description: 登录页
 **/
public class LoginMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     * 1. 通过 setUserData() 可以保存和传递数据
     * 2. 通过 getProperties().put("password","123"); 可以保存和传递数据
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(20);
        gridPane.setVgap(30);

        TextField userNameTf = new TextField();
        userNameTf.setPromptText("请输入用户名");
        //userNameTf.setFocusTraversable(false);
        userNameTf.setTooltip(new Tooltip("用户名"));
        userNameTf.setUserData("admin");

        PasswordField pwdPf = new PasswordField();
        pwdPf.setPromptText("请输入密码");
        //pwdPf.setFocusTraversable(false);
        pwdPf.setTooltip(new Tooltip("密码"));
        pwdPf.getProperties().put("password", "123");


        Button loginBtn = new Button("登录");
        Button resetBtn = new Button("重置");
        GridPane.setMargin(resetBtn, new Insets(0, 0, 0, 150));

        gridPane.add(new Label("用户名："), 0, 0);
        gridPane.add(new Label("密码："), 0, 1);
        gridPane.add(userNameTf, 1, 0);
        gridPane.add(pwdPf, 1, 1);
        gridPane.add(loginBtn, 0, 2);
        gridPane.add(resetBtn, 1, 2);


        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);
        primaryStage.setResizable(false);
        primaryStage.setTitle("登录页");
        primaryStage.show();


        //点击登录
        loginBtn.setOnAction(event -> {

            //验证用户名和密码

            String inputUserName = userNameTf.getText();
            String inputPwd = pwdPf.getText();

            if(Objects.equals(inputUserName,userNameTf.getUserData()) &&
                    Objects.equals(inputPwd,pwdPf.getProperties().get("password"))){
                System.out.println("登录成功");

                SuccessMain successMain = new SuccessMain(inputUserName, inputPwd);
                successMain.show();

            }else{

                //闪烁动效
                FadeTransition fadeTransition = new FadeTransition();
                //频率 1秒内完成
                fadeTransition.setDuration(Duration.seconds(0.1));
                fadeTransition.setFromValue(0);
                fadeTransition.setToValue(1);
                fadeTransition.setNode(gridPane);
                fadeTransition.play();
            }

        });


        //点击重置
        resetBtn.setOnAction(event -> {
            userNameTf.clear();
            pwdPf.clear();
        });


    }
}
